package com.foreknow.dao.impl;


import com.foreknow.dao.AdminDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Admin;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements AdminDao {
    /**
     *         //   基本语法
     *         //                INSERT INTO 语句可以有两种编写形式。
     *         //
     *         //                第一种形式无需指定要插入数据的列名，只需提供被插入的值即可：
     *         //
     *         //                INSERT INTO table_name
     *         //                VALUES (value1,value2,value3,...);
     *
     *         //                第二种形式需要指定列名及被插入的值：
     *         //
     *         //                INSERT INTO table_name (column1,column2,column3,...)
     *         //                VALUES (value1,value2,value3,...);
     * @param admin 对象
     * @return
     * @throws SQLException
     */
    @Override
    public boolean save(Admin admin) throws SQLException {
        //1. 把SQL语句
        String sql ="INSERT INTO admin(name,pass) VALUES(?,?)";
        //2. 连接数据库 调用DBManager类中的getConnection()方法
        //DBManager dbManager = DBManager.getInstance();
        //Connection conn = dbManager.getConnection();
        //3. 调用JdbcTemplate工具类中的添加方法  update
        //JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        //this.setConnection(conn); //调用父类BaseDao中的方法
        int i = jdbcTemplate.update(sql,admin.getName(),admin.getPass());
        return i == 1;
    }



    @Override
    public Admin queryById(int id) {
        //1. 把SQL语句
        String sql ="SELECT * FROM ADMIN WHERE ID=?";
        //2. 连接数据库 调用DBManager类中的getConnection()方法
        //DBManager dbManager = DBManager.getInstance();
        //Connection conn = dbManager.getConnection();
        //3. 调用JdbcTemplate工具类中的添加方法  update
        //JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        //jdbcTemplate.setConnection(conn);
        //第二个参数要从工厂类的容器中获取到AdminMapping对象
        //1. 获取工厂类的对象
        //MappingFactory mappingFactory = MappingFactory.getInstance();
        //2. 调用工厂类对象中的getMapping方法，根据key获取AdminMapping
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.ADMIN_MAPPING); //AdminMapping
        List<Object> list = jdbcTemplate.query(sql,mapping,id);
        Admin admin = null;
        if(list.size()>0) {
            admin = (Admin) list.get(0);
        }
        return admin;
    }


    @Override
    public boolean update(Admin admin) throws SQLException {
        //1. 把SQL语句
        String sql ="UPDATE ADMIN SET NAME=?,PASS=? WHERE ID=?";
        //2. 连接数据库 调用DBManager类中的getConnection()方法
        //DBManager dbManager = DBManager.getInstance();
        //Connection conn = dbManager.getConnection();
        //3. 调用JdbcTemplate工具类中的添加方法  update
        //JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        //jdbcTemplate.setConnection(conn);
        int i = jdbcTemplate.update(sql,admin.getName(),admin.getPass(),admin.getId());
        return i == 1;
    }

    @Override
    public boolean delete(int id) throws SQLException {
        //1. 把SQL语句
        String sql ="DELETE FROM ADMIN WHERE ID=?";
        //2. 连接数据库 调用DBManager类中的getConnection()方法
        //DBManager dbManager = DBManager.getInstance();
        //Connection conn = dbManager.getConnection();
        //3. 调用JdbcTemplate工具类中的添加方法  update
        //JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        //jdbcTemplate.setConnection(conn);
        int i = jdbcTemplate.update(sql,id);
        return i == 1;
    }

    @Override
    public List<Object> queryAll() {
        //1. 把SQL语句
        String sql ="SELECT * FROM ADMIN";
        //2. 连接数据库 调用DBManager类中的getConnection()方法
        //DBManager dbManager = DBManager.getInstance();
        //Connection conn = dbManager.getConnection();
        //3. 调用JdbcTemplate工具类中的添加方法  update
        //JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        //jdbcTemplate.setConnection(conn);
        //MappingFactory mappingFactory = MappingFactory.getInstance();
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.ADMIN_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping);
        return list;
    }

    @Override
    public Admin login(String name, String pass) {
        Admin admin = null;
        String sql = "select * from admin where name = ? and pass = ?";
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.ADMIN_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping,name,pass);
        if(list.size()>0){
            admin = (Admin) list.get(0);
        }
        return admin;
    }

    public static void main(String[] args) {
        AdminDao adminDao = new AdminDaoImpl();
        //        Admin admin = new Admin();
        //        admin.setName("xxx_testname");
        //        admin.setPass("111111");
        //        try {
        //            boolean isRight = adminDao.save(admin);
        //            if(isRight) {
        //                System.out.println("success......");
        //            }else {
        //                System.out.println("error......");
        //            }
        //        } catch (SQLException e) {
        //            e.printStackTrace();
        //        }
        //-------------------------------根据id查询-------------------------------
        Admin admin = adminDao.queryById(9);
        System.out.println(admin.getName());
    }
}
